package com.houduan.dao;

import com.houduan.model.Computer;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ComputerDao {

    private static final String URL = "jdbc:mysql://localhost:3306/netbar";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "rjxy123456";

    private static final String INSERT_COMPUTER_SQL = "INSERT INTO computers (name, type, status, price, description) VALUES (?, ?, ?, ?, ?)";
    private static final String SELECT_ALL_COMPUTERS = "SELECT * FROM computers";
    private static final String SELECT_COMPUTER_BY_ID = "SELECT * FROM computers WHERE id = ?";
    private static final String UPDATE_COMPUTER_SQL = "UPDATE computers SET name = ?, type = ?, status = ?, price = ?, description = ? WHERE id = ?";
    private static final String DELETE_COMPUTER_SQL = "DELETE FROM computers WHERE id = ?";

    private Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USERNAME, PASSWORD);
    }

    // 添加电脑
    public void addComputer(Computer computer) {
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(INSERT_COMPUTER_SQL)) {
            preparedStatement.setString(1, computer.getName());
            preparedStatement.setString(2, computer.getType());
            preparedStatement.setString(3, computer.getStatus());
            preparedStatement.setDouble(4, computer.getPrice());
            preparedStatement.setString(5, computer.getDescription());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 获取所有电脑
    public List<Computer> getAllComputers() {
        List<Computer> computers = new ArrayList<>();
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ALL_COMPUTERS);
             ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                Computer computer = new Computer(
                        resultSet.getInt("id"),
                        resultSet.getString("name"),
                        resultSet.getString("type"),
                        resultSet.getString("status"),
                        resultSet.getDouble("price"),
                        resultSet.getString("description")
                );
                computers.add(computer);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return computers;
    }

    // 根据 ID 获取电脑
    public Computer getComputerById(int id) {
        Computer computer = null;
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(SELECT_COMPUTER_BY_ID)) {
            preparedStatement.setInt(1, id);
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                if (resultSet.next()) {
                    computer = new Computer(
                            resultSet.getInt("id"),
                            resultSet.getString("name"),
                            resultSet.getString("type"),
                            resultSet.getString("status"),
                            resultSet.getDouble("price"),
                            resultSet.getString("description")
                    );
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return computer;
    }

    // 更新电脑信息
    public void updateComputer(Computer computer) {
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_COMPUTER_SQL)) {
            preparedStatement.setString(1, computer.getName());
            preparedStatement.setString(2, computer.getType());
            preparedStatement.setString(3, computer.getStatus());
            preparedStatement.setDouble(4, computer.getPrice());
            preparedStatement.setString(5, computer.getDescription());
            preparedStatement.setInt(6, computer.getId());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 删除电脑
    public void deleteComputer(int id) {
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(DELETE_COMPUTER_SQL)) {
            preparedStatement.setInt(1, id);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void updateStatus(int computerId, String status) {
        String updateStatusSql = "UPDATE computers SET status = ? WHERE id = ?";
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(updateStatusSql)) {
            preparedStatement.setString(1, status);
            preparedStatement.setInt(2, computerId);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    // Add this method to ComputerDao class
    public List<Computer> getFreeComputersByType(String type) {
        List<Computer> freeComputers = new ArrayList<>();
        String query = "SELECT * FROM computers WHERE type = ? AND status = '空闲'";

        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(query)) {
            preparedStatement.setString(1, type);
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                while (resultSet.next()) {
                    Computer computer = new Computer();
                    computer.setId(resultSet.getInt("id"));
                    computer.setName(resultSet.getString("name"));
                    computer.setType(resultSet.getString("type"));
                    computer.setStatus(resultSet.getString("status"));
                    computer.setPrice(resultSet.getDouble("price"));
                    computer.setDescription(resultSet.getString("description"));
                    freeComputers.add(computer);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return freeComputers;
    }
    public List<String> getAllComputerTypes() {
        List<String> computerTypes = new ArrayList<>();
        String query = "SELECT DISTINCT type FROM computers";

        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(query);
             ResultSet resultSet = preparedStatement.executeQuery()) {

            while (resultSet.next()) {
                computerTypes.add(resultSet.getString("type"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return computerTypes;
    }
    public List<Computer> getFreeComputers() {
        List<Computer> freeComputers = new ArrayList<>();
        String query = "SELECT * FROM computers WHERE status = '空闲'"; // 查询所有空闲电脑

        try (Connection connection = getConnection();
             PreparedStatement statement = connection.prepareStatement(query);
             ResultSet resultSet = statement.executeQuery()) {

            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String type = resultSet.getString("type");
                String status = resultSet.getString("status");
                double price = resultSet.getDouble("price");
                String description = resultSet.getString("description");

                Computer computer = new Computer(id, name, type, status, price, description);
                freeComputers.add(computer);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return freeComputers;
    }
}
